﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using WebApiV2.Data;
using WebApiV2.Data.Model;
using WebApiV2.Domain;

namespace WebApi.Realize
{
    public class Realize<T> : IRespository<T> where T : BaseEntity
    {

        protected readonly Admain1000 db;
        //生成对象
        private DbSet<T> _entity;

        private DbSet<T> Entity
        {
            get
            {
                if (_entity == null)
                {
                    _entity = db.Set<T>();

                }
                return _entity;
            }
        }

        public IQueryable<T> Table => Entity;

        public Realize(Admain1000 dbContext)
        {
            db = dbContext;
        }


        public void Deleted(T entity)
        {
            this.Entity.Remove(entity);
            db.SaveChanges();
        }

        public void Deleted(int id)
        {
            var row = Table.Where(x => x.id == id).FirstOrDefault();
            Deleted(row);
            db.SaveChanges();
        }


        public T GetbyId(int id)
        {
            return Entity.Where(x => x.id == id).FirstOrDefault();



        }

        public void Insert(T entity)
        {
            Entity.Add(entity);
            db.SaveChanges();
        }

        public void MoreInsert(IEnumerable<T> list)
        {
            Entity.AddRange(list);
            db.SaveChanges();
        }

        public void Update(T entity)
        {
            Entity.Update(entity);
            db.SaveChanges();
        }

     
    }
}
